﻿//连接数据库
var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',//ip地址
    user: 'root',//用户名
    password: '123456',//密码
    database:'test'//数据库名字
});
connection.connect();

//增加
var  userAddSql = 'INSERT INTO user(name,password) VALUES(?,?)';
var  userAddSql_Params = ['haha', 'test'];
//增 add
connection.query(userAddSql,userAddSql_Params,function (err, result) {
        if(err){
         console.log('[INSERT ERROR] - ',err.message);
         return;
        }       
       console.log('-------INSERT----------');  
       console.log('INSERT ID:',result);       
       console.log('-------INSERT END----------');
});
//修改
var userModSql = 'UPDATE user SET password = ? WHERE name = ?';
var userModSql_Params = ['test1','haha'];
//改 up
connection.query(userModSql,userModSql_Params,function (err, result) {
   if(err){
         console.log('[UPDATE ERROR] - ',err.message);
         return;
   }       
  console.log('----------UPDATE-------------');
  console.log('UPDATE affectedRows',result.affectedRows);
  console.log('----------UPDATE END-------------');
});
//查询
connection.query('select * from user', function(err, result) {
    if (err) throw err;
	console.log('---------------SELECT----------------');
    console.log('查询结果为: ', result);
	console.log('---------------SELECT END----------------');
});
//删除
var  userDelSql = "DELETE FROM user WHERE name = ?";
var  userDelSql_Params = ['haha'];
connection.query(userDelSql,userDelSql_Params,function (err, result) {
        if(err){
          console.log('[DELETE ERROR] - ',err.message);
          return;
        }       
       console.log('-------------DELETE--------------');
       console.log('DELETE affectedRows',result.affectedRows);
       console.log('-------------DELETE END--------------');
});
//关闭连接
connection.end();